using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Portal.Models;
using ST.Application.MenuServices;
using ST.Application.MenuServices.Dtos;

namespace Portal.Controllers
{
    public class MenuController : ControllerBase
    {
        private readonly IMenuService _menuService;

        public MenuController(IMenuService menuService)
        {
            _menuService = menuService;
        }
        // GET
        public IActionResult Index()
        {
            return View();
        }
        
        [HttpGet]
        public async Task<IActionResult> GetMenuTreeData()
        {
            var menus = await _menuService.GetAllListAsync();
            var treeModels = menus.Select(menu => new TreeModel() {Id = menu.Id.ToString(), Text = menu.Name, Parent = menu.ParentId == Guid.Empty ? "#" : menu.ParentId.ToString()}).ToList();
            return Json(treeModels);
        }
        
        public async Task<IActionResult> GetMenusByParent(Guid parentId, int startPage, int pageSize)
        {
            var result = await _menuService.GetMenusByParent(parentId, startPage, pageSize);
            return Json(result);
        }
        
        public async Task<IActionResult> Edit(MenuDto dto)
        {
            if (!ModelState.IsValid)
            {
                return Json(new
                {
                    Result = "Faild",
                    Message = GetModelStateError()
                });
            }

            var result = false;
            if (await _menuService.GetAsync(dto.Id) == null)
            {
                result = await _menuService.InsertAsync(dto);
            }
            else
            {
                result = await _menuService.UpdateAsync(dto);
            }

            return Json(result ? new { Result = "Success" } : new { Result = "Faild" });
        }

        public async Task<IActionResult> DeleteMuti(string ids)
        {
            string[] idArray = ids.Split(',');
            try
            {
                var delIds = idArray.Select(Guid.Parse).ToList();
                await _menuService.DeleteBatchAsync(delIds);
                return Json(new
                {
                    Result = "Success"
                });
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    Result = "Faild",
                    Message = ex.Message
                });
            }
        }
        public async Task<IActionResult> Delete(Guid id)
        {
            try
            {
                await _menuService.DeleteAsync(id);
                return Json(new
                {
                    Result = "Success"
                });
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    Result = "Faild",
                    Message = ex.Message
                });
            }
        }
        public async Task<IActionResult> Get(Guid id)
        {
            var dto = await _menuService.GetAsync(id);
            return Json(dto);
        }
    }
}